package com.co.vote.web.controller.vote;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.co.common.utils.PageReq;
import com.co.server.constant.Constant;
import com.co.server.entity.vote.TChoice;
import com.co.server.entity.vote.TClass;
import com.co.server.entity.vote.TComment;
import com.co.server.entity.vote.TVote;
import com.co.server.entity.vote.TVoteItem;
import com.co.server.entity.vote.TVoteLabel;
import com.co.server.entity.vote.aide.TChoiceQuery;
import com.co.server.entity.vote.aide.TClassConstent;
import com.co.server.entity.vote.aide.TClassQuery;
import com.co.server.entity.vote.aide.TCommentConstent;
import com.co.server.entity.vote.aide.TCommentQuery;
import com.co.server.entity.vote.aide.TVoteForm;
import com.co.server.entity.vote.aide.TVoteItemQuery;
import com.co.server.entity.vote.aide.TVoteLabelQuery;
import com.co.server.entity.vote.aide.TVoteQuery;
import com.co.server.entity.vote.aide.VoteForm;
import com.co.server.service.vote.TChoiceService;
import com.co.server.service.vote.TClassService;
import com.co.server.service.vote.TCommentService;
import com.co.server.service.vote.TVoteItemService;
import com.co.server.service.vote.TVoteLabelService;
import com.co.server.service.vote.TVoteService;
import com.co.server.service.vote.TVoteUserService;
import com.co.vote.constant.HttpStatusCode;
import com.co.vote.utils.ClientUtil;
import com.co.vote.utils.SessionUtil;

@Controller
@RequestMapping("/vote")
public class VoteController {

	@Resource
	private TVoteService tVoteService;
	@Resource
	private TVoteItemService tVoteItemService;
	@Resource
	private TVoteLabelService tVoteLabelService;
	@Resource
	private TCommentService tCommentService;
	@Resource
	private TVoteUserService tVoteUserService;
	@Resource
	private TClassService tClassService;
	@Resource
	private TChoiceService tChoiceService;
	/**
	 * 投票列表
	 * @param model
	 * @param session
	 * @param pageReq
	 * @param query
	 * @return
	 */
	@RequestMapping(value = "/list", method = { RequestMethod.GET, RequestMethod.POST })
	public String list( Model model,HttpSession session,PageReq pageReq,TVoteQuery query ) {
		pageReq.setPageSize(10);
		query.setCreateUserId(SessionUtil.getUserId(session));
		query.setStatus(Constant.STATUS_ACTIVE);
		query.setDead(null);
		pageReq.setSort(new Sort(Direction.DESC,"t.create_time"));
		Page<TVote> page = tVoteService.queryPageList(query, pageReq);
		model.addAttribute("page", page);
		return "/vote/list";
	}

	/**
	 * 新增投票
	 * @param model
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "/add", method = { RequestMethod.GET,RequestMethod.POST })
	public String addInit( Model model,HttpSession session) {
		TClassQuery tClassQuery = new TClassQuery();
		tClassQuery.setStatus(TClassConstent.CLASS_STATUS_ACTIVE);
		List<TClass> classList = tClassService.queryList(tClassQuery);
		model.addAttribute("classList",classList);
		return "vote/add";
	}
	
	/**
	 * 新增投票
	 * @param model
	 * @param session
	 * @param tVote
	 * @param pageReq
	 * @param redirectAttributes
	 * @return
	 */
	@RequestMapping(value = "/save", method = { RequestMethod.GET,RequestMethod.POST })
	public String save(Model model,HttpSession session,VoteForm voteForm ,
			PageReq pageReq,RedirectAttributes redirectAttributes,TClassQuery classQuery) {
			voteForm.setCreateUserId(SessionUtil.getUserId(session));
			tVoteService.addVote4Form(voteForm);
		redirectAttributes.addAttribute("pageNumber", pageReq.getPageNumber());
		return "redirect:/vote/list";
	}
	
	@RequestMapping(value = "/show/{id}", method = { RequestMethod.GET,RequestMethod.POST })
	public String show( Model model,HttpSession session,HttpServletRequest request,@PathVariable Integer id) {
		//投票信息
		TVoteQuery tVoteQuery = new TVoteQuery();
		tVoteQuery.setId(id);
		tVoteQuery.setStatus(Constant.STATUS_ACTIVE);
		TVote vote = tVoteService.queryOne(tVoteQuery);
		
		if(vote==null){
			return "error/403Error";
		}
		model.addAttribute("vote", vote);
		
		//投票类目
		TVoteItemQuery tVoteItemQuery = new TVoteItemQuery();
		tVoteItemQuery.setVoteId(id);
		List<TVoteItem> voteItemList = tVoteItemService.queryList(tVoteItemQuery);
		model.addAttribute("voteItemList", voteItemList);
		//设置百分数
		tVoteItemService.setPrecent(id, voteItemList);
		
		//查看是否已经投票
		Integer userId= SessionUtil.getUserId(session);
		String ip = ClientUtil.getIp(request);
		Boolean voteFlg = tVoteService.getVoteFlg(userId,ip,id);
		if(voteFlg){
			model.addAttribute("hasvote", true);
		}
		
		//本投票的关键词
		TVoteLabelQuery tVoteLabelQuery = new TVoteLabelQuery();
		tVoteLabelQuery.setVoteId(id);
		List<TVoteLabel> voteLabelList = tVoteLabelService.queryList(tVoteLabelQuery);
		model.addAttribute("voteLabelList", voteLabelList);
		
		//相关投票
//		TVoteQuery tVoteQuery = new TVoteQuery();
//		List<TVote> relativeVoteList = new ArrayList<TVote>();
		//根据分类查询 (暂时不再加进相关，会出现Id重复)
//		Integer classId = vote.getClassId();
//		tVoteQuery.setClassId(classId);
//		relativeVoteList.addAll(tVoteService.queryList(tVoteQuery));
		//根据关键词查询
//		Collection<Integer> voteIds = tVoteLabelService.queryVoteIdsByVoteId(id);
//		tVoteQuery.setClassId(null);
//		tVoteQuery.setVoteIds(voteIds);
//		relativeVoteList.addAll(tVoteService.queryList(tVoteQuery));
//		model.addAttribute("relativeVoteList", relativeVoteList);
		
		//投票评论
		PageReq pageComment = new PageReq();
		pageComment.setSort(new Sort(Direction.DESC,"t.create_time"));
		 Page<TComment> commentPage = tCommentService.queryPageList(new TCommentQuery(id,TCommentConstent.COMMENT_STATUS_ACTIVE),pageComment);
		model.addAttribute("commentPage", commentPage);
		
		TChoiceQuery tChoiceQuery = new TChoiceQuery();
		//查询是否已经点赞
		tChoiceQuery.setIp(ip);
		tChoiceQuery.setUserId(SessionUtil.getUserId(session));
		tChoiceQuery.setVoteId(id);
		TChoice choiceOld = tChoiceService.queryOne(tChoiceQuery);
		if(choiceOld!=null){
			model.addAttribute("knockDone", true);
		}
		
		return "vote/show";
	}
	//异步加载评论
	@ResponseBody
	@RequestMapping(value = "/ajaxList", method = { RequestMethod.GET, RequestMethod.POST })
	public Map<String, Object> ajaxList( Model model,HttpSession session,PageReq pageReq,TCommentQuery query ) {
		query.setStatus(TCommentConstent.COMMENT_STATUS_ACTIVE);
		pageReq.setSort(new Sort(Direction.DESC,"t.create_time"));
		Page<TComment> page = tCommentService.queryPageList(query, pageReq);
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("code", HttpStatusCode.CODE_SUCCESS);
		result.put("page", page);
		return result;
	}
		
	/**
	 * 投票
	 * @param model
	 * @param request
	 * @param session
	 * @param form
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/doVote", method = { RequestMethod.GET,RequestMethod.POST })
	public Map<String, Object> doVote( Model model,HttpServletRequest request,HttpSession session,TVoteForm form) {
		
		tVoteService.doVote(form,SessionUtil.getUserId(session),ClientUtil.getIp(request));
		Integer voteId = form.getId();
		//投票类目
		TVoteItemQuery tVoteItemQuery = new TVoteItemQuery();
		tVoteItemQuery.setVoteId(voteId);
		List<TVoteItem> voteItemList = tVoteItemService.queryList(tVoteItemQuery);
		//设置百分数
		tVoteItemService.setPrecent(voteId, voteItemList);
		
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("code", HttpStatusCode.CODE_SUCCESS);
		result.put("voteItemList", voteItemList);
		
		
		return result;
	}

	
	/**
	 * 点赞，点踩
	 * @param model
	 * @param request
	 * @param session
	 * @param tChoice
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/doKnock", method = { RequestMethod.GET,RequestMethod.POST })
	public Map<String, Object> doKnock( Model model,HttpServletRequest request,HttpSession session,TChoice tChoice) {
		tChoice.setIp(ClientUtil.getIp(request));
		tChoice.setUserId(SessionUtil.getUserId(session));
		tVoteService.doKnock(tChoice);
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("code", HttpStatusCode.CODE_SUCCESS);
		return result;
	}
	
	/**
	 * 切换到首选投票
	 * @param model
	 * @param request
	 * @param session
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/doChange", method = { RequestMethod.GET,RequestMethod.POST })
	public Map<String, Object> doChange( Model model,HttpServletRequest request,HttpSession session) {
		TVote defaultVote = tVoteService.getDefaultVote(SessionUtil.getUserSession(session), new PageReq(),ClientUtil.getIp(request));
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("code", HttpStatusCode.CODE_SUCCESS);
		result.put("voteId", defaultVote.getId());
		return result;
	}
	
}
	
